/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RedaccionRecomendaciones.java
 *
 * Created on 02/10/2011, 08:25:24 PM
 */

package GUI.AdministracionProyectos;

import BE.Proyecto;
import BE.Recomendacion;
import BL.ProyectoBl;
import BL.RecomendacionBl;
import GUI.AdministracionUsuarios.UtilUsuarios;
import GUI.Principal;
import javax.swing.JOptionPane;

/**
 *
 * @author CarLita
 */
public class RedaccionRecomendaciones extends javax.swing.JFrame {
    int idProyecto;
    int modoVentana;
    Recomendacion recomendacion;
    Principal ventanaPrincipal;
    
    public RedaccionRecomendaciones(int idProyecto, Principal ventanaPrincipal) {
        initComponents();
        this.idProyecto = idProyecto;
        modoVentana = Principal.MODO_CREACION;
        setearModo();
        bloquearPorPermisos();
    }
    
    public RedaccionRecomendaciones(int idProyecto, int modoVentana, Recomendacion recomendacion, Principal ventanaPrincipal){
        initComponents();
        this.idProyecto = idProyecto;
        this.modoVentana = modoVentana;
        this.recomendacion = recomendacion;
        setearModo();
        bloquearPorPermisos();
    }
    
    private void setearModo(){
        switch(modoVentana){
            case Principal.MODO_CREACION:
                btnEditar.setVisible(false);
                btnLimpiar.setVisible(true);
                btnGuardar.setVisible(true);                
                break;
            case Principal.MODO_EDICION:
                llenarInformacion();
                btnEditar.setVisible(false);
                btnLimpiar.setVisible(false);
                btnGuardar.setVisible(true);
                break;
            case Principal.MODO_LECTURA:
                llenarInformacion();
                btnEditar.setVisible(true);
                btnLimpiar.setVisible(false);
                btnGuardar.setVisible(false);
                break;
        }
    }
    
    private void llenarInformacion(){
        cmbScore.setSelectedIndex(recomendacion.getScore());
        txtRecomendacion.setText(recomendacion.getRecomendacion());
        txtTitulo.setText(recomendacion.getTitulo());
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnCancelar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtRecomendacion = new javax.swing.JTextArea();
        btnEditar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        btnLimpiar = new javax.swing.JButton();
        btnGuardar = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        cmbScore = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        txtTitulo = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/AdministracionPlantillas/cancelar.png"))); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        txtRecomendacion.setColumns(20);
        txtRecomendacion.setFont(new java.awt.Font("Tahoma", 0, 11));
        txtRecomendacion.setRows(5);
        jScrollPane1.setViewportView(txtRecomendacion);

        btnEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/AdministracionPlantillas/edit.gif"))); // NOI18N
        btnEditar.setText("Editar");

        jLabel1.setText("Recomendación:");

        jLabel4.setFont(new java.awt.Font("Verdana", 0, 24));
        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/imagenes/lista-de-regalos.png"))); // NOI18N
        jLabel4.setText("Recomendaciones");

        btnLimpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/AdministracionPlantillas/limpiar.png"))); // NOI18N
        btnLimpiar.setText("Limpiar");
        btnLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimpiarActionPerformed(evt);
            }
        });

        btnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/AdministracionPlantillas/guardar.png"))); // NOI18N
        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        jLabel9.setText("Score:");

        cmbScore.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-", "1", "2", "3", "4", "5" }));

        jLabel2.setText("Título:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(jLabel4))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(txtTitulo)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 359, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel9)
                                .addGap(65, 65, 65)
                                .addComponent(cmbScore, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(27, 27, 27)
                        .addComponent(btnLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbScore, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnEditar)
                    .addComponent(btnGuardar)
                    .addComponent(btnLimpiar)
                    .addComponent(btnCancelar))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.dispose();
}//GEN-LAST:event_btnCancelarActionPerformed

private void btnLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimpiarActionPerformed
    limpiar();
}//GEN-LAST:event_btnLimpiarActionPerformed

private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
    guardar();
}//GEN-LAST:event_btnGuardarActionPerformed

private void guardar(){
    recomendacion = new Recomendacion();
    Proyecto p = new ProyectoBl().buscarPorId(idProyecto);
    recomendacion.setProyecto(p);
    recomendacion.setRecomendacion(txtRecomendacion.getText().trim());
    recomendacion.setScore(cmbScore.getSelectedIndex());
    recomendacion.setTitulo(txtTitulo.getText().trim());
    if (new RecomendacionBl().guardar(recomendacion)){          
        JOptionPane.showMessageDialog(this, "Se ha guardado la recomendación", "Grabación", JOptionPane.INFORMATION_MESSAGE);        
    };
}

private void limpiar() {
    cmbScore.setSelectedIndex(0);
    txtRecomendacion.setText("");
    txtTitulo.setText("");
}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JButton btnLimpiar;
    private javax.swing.JComboBox cmbScore;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea txtRecomendacion;
    private javax.swing.JTextField txtTitulo;
    // End of variables declaration//GEN-END:variables

    private void bloquearPorPermisos() {
       if (ventanaPrincipal.getPermisos().getPermisoProyecto().indexOf(UtilUsuarios.VER)>-1){
            btnEditar.setEnabled(false);
            btnEditar.setVisible(false);
            btnGuardar.setEnabled(false);
            btnGuardar.setVisible(false);
       }        
        if (ventanaPrincipal.getPermisos().getPermisoProyecto().indexOf(UtilUsuarios.CREAR)>-1){
            btnEditar.setEnabled(false);
            btnEditar.setVisible(false);
            btnGuardar.setEnabled(true);
            btnGuardar.setVisible(true);
        }                
        if (ventanaPrincipal.getPermisos().getPermisoProyecto().indexOf(UtilUsuarios.EDITAR)>-1){
            btnEditar.setEnabled(true);
            btnEditar.setVisible(true);
            btnGuardar.setEnabled(true);
            btnGuardar.setVisible(true);
        }
      }



}
